package com.itheima.es;

import com.alibaba.fastjson.JSON;
import com.itheima.es.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 演示: 操作es客户端
 * 文档的搜索
 * 1. 创建请求对象  SearchRequest
 * 1.1 整个请求参数对象通过 SearchSourceBuilder 构建
 * 1.2 builder.query 构建查询条件
 *          QueryBuilders工具 可以快捷的构建查询条件
 * 1.3 builder.sort 构建排序条件
 * 1.4 builder.from 构建分页条件
 * 1.5 builder.size
 * 1.6 builder.highlight  构建高亮条件
 * 1.7 builder.aggregation 构建聚合条件
 *
 * searchRequest.source(builder); // 一定要放到请求中
 * 2. 执行对应方法  client.search
 * 3. 解析SearchResponse 搜索结果
 *    SearchResponse
 *      response.getHits.getTotal.value 总数量
 *      response.getHits.getHits 得到文档数组
 *                          遍历文档数组
 *                               每一个元素 获取里面source字段内容
 * @作者 itcast
 * @创建日期 2021/3/19 9:09
 **/
public class EsDemo03 {
    RestHighLevelClient client;

    /**
     * note: 唱歌或javaee
     *
     * match:
     *
     * @throws IOException
     */
    @Test
    public void match() throws IOException {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery("note","唱歌或javaee"));
        printResult(builder);
    }

    /**                  bool
     * note:  同学           match:     must
     * age:  18 ~ 28        range:      filter
     * gender:  1           term:       filter
     * @throws IOException
     */
    @Test
    public void bool() throws IOException {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 创建bool的查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//     * note:  同学           match:     must
        boolQueryBuilder.must(QueryBuilders.matchQuery("note","同学"));
//     * age:  18 ~ 28        range:      filter
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("age").gte(18).lte(28));
//      * gender:  1           term:       filter
        boolQueryBuilder.filter(QueryBuilders.termQuery("gender","0"));
        builder.query(boolQueryBuilder);// 传入bool条件对象
        printResult(builder);
    }


    @Test
    public void matchAll() throws IOException {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());
        printResult(builder);
    }

    public void printResult(SearchSourceBuilder builder) throws IOException {
        //      * 1. 创建请求对象  SearchRequest
        SearchRequest request = new SearchRequest("user");
//      * 1.1 整个请求参数对象通过 SearchSourceBuilder 构建
        request.source(builder); // 一定要放到请求中
//      *  2. 执行对应方法  client.search
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//      *  3. 解析SearchResponse 搜索结果
        SearchHits hits = response.getHits();
        System.out.println("查询到的总数量: " + hits.getTotalHits().value);
//      *    SearchResponse
//                *      response.getHits.getTotal.value 总数量
//                *
        SearchHit[] docArray = hits.getHits();
//       *      response.getHits.getHits 得到文档数组
        for (SearchHit doc : docArray) {
            String userJson = doc.getSourceAsString();
            User user = JSON.parseObject(userJson, User.class);
            System.out.println(user);
        }
//       *                          遍历文档数组
//                *                               每一个元素 获取里面source字段内容
    }



    @Before
    public void init(){
        // 创建操作es的客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.129",9200,"http")
                )
        );
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        // 创建操作es的客户端
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("192.168.200.129",9200,"http")
//                )
//        );
//        // 使用client操作es
//
//        // 使用完毕后需要释放链接
//        try {
//            client.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
